<?php include "../public/header.php";?>
<?php include "../public/header_edit.php";?>
<style>
    #operatePage .main-container .short_table_list tbody{max-height: 80%;}
    .del{padding-left: 5px;color: red;cursor: pointer;}
    .uploads{text-align: center;color: #65b5ff;cursor: pointer;}
    .showup{color: #65b5ff;cursor: pointer;}
    input{border-color: #ccc !important;}
</style>
<body>
<div class="layui-form layui-form-pane" id="operatePage">
    <div class="page-title" id="page-title">
        <div class="page-title-con">
            <span class="title">新建副本</span>
            <div class="move_title"></div>
            <div class="page-title-opt-list">
                <button class="layui-btn layui-btn-sm btn-dark marginr5" type="button" lay-submit="" lay-filter="add">确定</button>
                <span id="page-close" class="page-close" onclick="layer_close()">×</span>
            </div>
        </div>
    </div>
    <div class="main-container">
        <div class="layui-form-item">
            <div class="layui-row layui-col-space10">
                <div class="layui-col-md12">
                    <label class="layui-form-label">证书名称</label>
                    <div class="layui-input-block">
                        <input type="text" id="name" readonly autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-col-md12" id="numberDiv">
                    <label class="layui-form-label">新增副本数量</label>
                    <div class="layui-input-block">
                        <input type="number" id="number" oninput="this.value=this.value.replace(/\D/g,'')" placeholder="请填写整数" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-col-md12 height_auto none" id="listDiv">
                    <div class="short_table_list" key="1">
                        <table class="layui-table" lay-size="sm">
                            <thead>
                            <tr>
                                <th style="width: 35px;text-align: center;">序号</th>
                                <th>证书编号</th>
                                <th>自编号</th>
                                <th>附件</th>
                            </tr>
                            </thead>
                            <tbody class="short_table_tbody">
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    var param_id = getQueryVariable('id');
    var param_name = decodeURI(getQueryVariable('name'));
    $('#name').val(param_name);

    function del(obj) {
        $(obj).parent().remove();
    }

    function upload(obj, index){
        open_page('../public/upload_copy.php?index='+index,'附件','600px','400px');
    }

    var viewer = null;
    function viewinit() {
        viewer = new Viewer(document.getElementById('listDiv'));
    }
    $('#listDiv').on('click','.showimg',function () {
        viewer.show();
    });


    layui.use(['laytp', 'form'], function() {
        var form = layui.form;
        form.on('submit(add)', function(data){
            if($('#listDiv').is(':hidden')){
                var number = $('#number').val();
                if(!number){
                    facade.error('请填写副本数量'); return false;
                }

                facade.ajax({url: ask_cert_company_copy, type: 'get', data:{id: param_id, number: number}}).done(function(res){
                    if (res.code === 2000) {
                        var list = res.data;
                        var str = '';
                        list.forEach(function (item, index) {
                            str += '<tr>';
                            str += '<td>'+(index + 1)+'</td>';
                            str += '<td><input type="text" class="layui-input number" value="'+item+'"></td>';
                            str += '<td><input type="text" class="layui-input zinumber"></td>';
                            str += '<td class="file_upload">';
                            str += '<div class="uploadDiv">';
                            str += '</div>';
                            str += '<div class="uploads" onclick="upload(this, '+(index+1)+')">上传</div>';
                            str += '</td>';
                            str += '</tr>';
                        });
                        $('.short_table_tbody').html(str);
                        $('#listDiv').show();
                        $('#numberDiv').hide();
                    }
                });
            }else{
                let list = [];
                $('.short_table_tbody tr').each(function () {
                    let number = $(this).find('.number').val();
                    let zinumber = $(this).find('.zinumber').val();
                    var files = [''];
                    $(this).find('.uploadDiv').find('div').each(function () {
                        var id = $(this).attr('key');
                        files.push({id: id, fileplain: ''});
                    });
                    let arr = {
                        number: number,
                        zinumber: zinumber,
                        files:files
                    };
                    list.push(arr);
                });

                facade.ajax({url: ask_cert_company_copy, data:{list: list, id: param_id}}).done(function(res){
                    if (res.code === 2000) {
                        facade.success('操作成功');
                        setTimeout(function () {
                            parent.window.location.reload();
                        },1500);
                    }
                });
            }
            return false;
        });

    });
</script>
</body>
</html>
